//
//  CardViewController.m
//  day-07
//
//  Created by hcios on 2017/7/9.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CardViewController.h"
#import "CardModel.h"
#import "AFNetworking.h"
#import "CardCollectionViewCell.h"
#import "RGCardViewLayout.h"

//跳转页面
#import "XYJGLViewController.h"
#import "XYJLXViewController.h"
#import "XYJXCViewController.h"
#import "XYJZTViewController.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)


@interface CardViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,CardCollectionViewCellDelegate>

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UICollectionView *cityCV;

@end

static NSString *const CollectionViewCellIdentifier = @"cellID";

@implementation CardViewController

-(instancetype)init{
    if (self = [super init]) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@攻略",_cityName];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initCity];
    [self netWorking];
}

-(void)initCity{
    
    RGCardViewLayout *layout = [[RGCardViewLayout alloc] init];
    
    self.cityCV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -50, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    self.cityCV.delegate = self;
    self.cityCV.dataSource = self;
    
    self.cityCV.pagingEnabled = YES;
    [self.cityCV registerNib:[UINib nibWithNibName:@"CardCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
    self.cityCV.backgroundColor = RGB(214, 226, 238);
    [self.view addSubview:self.cityCV];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 1;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  _dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CardCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.section];
    
    cell.delegate = self;
    return cell;
}


#pragma mark - btn点击事件
//攻略
-(void)gl:(id)sender{
    
    NSLog(@"点击攻略");
    
    //获取button的点击事件饿indexPath
    UIButton *btn = (UIButton *)sender;
    CGPoint point = btn.center;
    point = [self.cityCV convertPoint:point fromView:btn.superview];
    NSIndexPath *indexPath = [self.cityCV indexPathForItemAtPoint:point];
    CardModel *model = _dataArray[indexPath.section];
    XYJGLViewController *VC = [[XYJGLViewController alloc] init];
    VC.selfID = model.city_id;
    VC.cityName = model.city_name_zh_cn;
    [self.navigationController pushViewController:VC animated:YES];
    
}


-(void)xc:(id)sender{
    
    NSLog(@"点击行程");
    UIButton *btn = (UIButton *)sender;
    CGPoint point = btn.center;
    point = [self.cityCV convertPoint:point fromView:btn.superview];
    NSIndexPath *indexPath = [self.cityCV indexPathForItemAtPoint:point];
    CardModel *model = _dataArray[indexPath.section];
    XYJXCViewController *VC = [[XYJXCViewController alloc] init];
    VC.selfID = model.city_id;
    [self.navigationController pushViewController:VC animated:YES];
}


-(void)lxd:(id)sender{
    
    NSLog(@"点击旅行地");
    UIButton *btn = (UIButton *)sender;
    CGPoint point = btn.center;
    point = [self.cityCV convertPoint:point fromView:btn.superview];
    NSIndexPath *indexPath = [self.cityCV indexPathForItemAtPoint:point];
    CardModel *model = _dataArray[indexPath.section];
    XYJLXViewController *VC = [[XYJLXViewController alloc] init];
    VC.selfID = model.city_id;
    VC.cityName = model.city_name_zh_cn;
    [self.navigationController pushViewController:VC animated:YES];
}


-(void)zt:(id)sender{
    
    NSLog(@"点击专题");
    UIButton *btn = (UIButton *)sender;
    CGPoint point = btn.center;
    point = [self.cityCV convertPoint:point fromView:btn.superview];
    NSIndexPath *indexPath = [self.cityCV indexPathForItemAtPoint:point];
    CardModel *model = _dataArray[indexPath.section];
    XYJZTViewController *VC = [[XYJZTViewController alloc] init];
    
    VC.selfID = model.city_id;
    VC.cityName = model.city_name_zh_cn;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld%ld",(long)[indexPath section],(long)[indexPath row]);
    CardModel * model = _dataArray[indexPath.section];
    _selfID = model.city_id;
}


-(void)netWorking{
    
    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json",_selfID];
    
    NSDictionary *param = @{@"page":@"1"};
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        for (NSDictionary *dict in responseObject) {
            CardModel *model = [[CardModel alloc] initWithDict:dict];
            ;
            [_dataArray addObject:model];
        }
        [self.cityCV reloadData];
      
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"网络请求失败");
    }];
}


@end
